package ntd.util.frame.magic.api;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.c.Constant;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.config.MagicFunction;
import org.ssssssss.script.annotation.Comment;
import org.ssssssss.script.annotation.Function;
import org.ssssssss.script.functions.DateExtension;

import java.time.LocalDateTime;
import java.util.Date;

@Component  //注入到Spring容器中
public class MyCustomFunction implements MagicFunction {

    @Function
    @Comment("日期格式化")
    public static String date_format(@Comment("目标日期") Date target) {
        return target == null ? null : DateExtension.format(target, "yyyy-MM-dd HH:mm:ss");
    }

    @Function
    @Comment("日期格式化")
    public static String date_format(@Comment("目标日期") Date target, @Comment("格式") String pattern) {
        return target == null ? null : DateExtension.format(target, pattern);
    }

    @Function
    @Comment("日期格式化")
    public static String date_format(@Comment("目标日期") LocalDateTime target) {
        return LocalDateTimeUtil.format(target, Constant.DATE_FORMAT_STAND);
    }

    @Function
    @Comment("判断值是否为空")
    public static Object ifnull(@Comment("目标值") Object target, @Comment("为空的值") Object trueValue, @Comment("不为空的值") Object falseValue) {
        return target == null ? trueValue : falseValue;
    }

    @Function
    public static String toJsonStr(Object target) {
        return JSONUtil.toJsonStr(target);
    }

    @Function
    public static Object toJsonObj(String str) {
        if (JSONUtil.isJsonArray(str)) {
            return JSONUtil.parseArray(str);
        }
        return JSONUtil.parseObj(str);
    }



}